using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.Instructions;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif
using Gnome.Entities;
using Gnome.Engine;
using FlatRedBall.Audio;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Gnome.Screens
{

    public partial class LevelBase
    {
        protected bool isPaused = false;
        bool DEBUG = false;
        public static float SCREEN_WIDTH = SpriteManager.Camera.AbsoluteRightXEdgeAt(1) * 2;
        public static float SCREEN_HEIGHT = SpriteManager.Camera.AbsoluteTopYEdgeAt(1) * 2;

        private int MAX_MULTIPLIER = 5;

        private float MAX_WEIGHT = 20;
        //private float MAX_2_WEIGHT = 20;
        private int COLLISION_WAIT = 75;
        private float ITEM_RESPAWN_TIMER = 0;
        private float PWRUP_RESPAWN_TIMER = 3;
        private float TIME_RESPAWN_TIMER;
        private int TARGET_SCORE = 3000;
        private float MAX_SCORE = 9999;
        private int TIME_ADDED = 30;
        private float ITEM_SPRITE_WIDTH = 2.5f;
        private float ITEM_SPRITE_HEIGHT = 2.5f;
        private int MIN_POWERUP_SEC = 20;
        private int MAX_POWERUP_SEC = 35;
        private int MAX_TIME_PICKUPS = 3;
        private int MIN_TIME_RESPAWN = 10;
        private int MAX_TIME_RESPAWN = 18;
        private float MAX_LEVEL_TIME = 99;

        Sound squak1 = AudioManager.GetSound("squak1");
        Sound squak3 = AudioManager.GetSound("squak3");
        Sound squak4 = AudioManager.GetSound("squak4");
        Sound dropoff = AudioManager.GetSound("dropoff2");
        Sound grass = AudioManager.GetSound("grass_rustling1");

        //protected Gnome.Screens nextScreen;

        FlatRedBall.Graphics.Layer obstaclelayer;
        FlatRedBall.Graphics.Layer hidinglayer;
        FlatRedBall.Graphics.Layer itemlayer;
        FlatRedBall.Graphics.Layer playerlayer;
        FlatRedBall.Graphics.Layer birdlayer;
        FlatRedBall.Graphics.Layer effectslayer;
        FlatRedBall.Graphics.Layer hidingeffectslayer;
        protected FlatRedBall.Graphics.Layer instructlayer;
        protected FlatRedBall.Graphics.Layer hudlayer;

        private static List<Texture2D> obstacleTextures = new List<Texture2D>
        {
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/big_puddle.png"),
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/puddle.png"),
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/puddle2.png"),
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/puddle3.png"),
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/tallgrass.png")
        };
        private static List<Texture2D> hidingTextures = new List<Texture2D>
        {
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/appletree.png"),
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/tree.png"),
            FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/bushes.png")
        };

        private static Dictionary<Texture2D, Obstacle.VariableState> obstacleTextureTypeMap = new Dictionary<Texture2D, Obstacle.VariableState>
        {
            {obstacleTextures[0], Obstacle.VariableState.Mud},
            {obstacleTextures[1], Obstacle.VariableState.Mud},
            {obstacleTextures[2], Obstacle.VariableState.Mud},
            {obstacleTextures[3], Obstacle.VariableState.Mud},
            {obstacleTextures[4], Obstacle.VariableState.TallGrass}
        };

        private static Dictionary<Texture2D, HidingSpot.VariableState> hidingTextureTypeMap = new Dictionary<Texture2D, HidingSpot.VariableState>
        {
            {hidingTextures[0], HidingSpot.VariableState.Tree},
            {hidingTextures[1], HidingSpot.VariableState.Bush}
        };

        private bool playerCaughtByBird = false;
        private int endgame_counter = 150;

        public bool pauseforinstructions = GlobalData.FirstGame;


        public List<Texture2D> Textures = new List<Texture2D>
        {
            //FlatRedBallServices.Load<Texture2D>("blahblah.bmp")
        };

        public void CustomInitialize()
        {
            GlobalData.ClearedLevel = false;
            isTwoPlayers = GlobalData.is2P;

            string contentManagerName = "ContentManager";
            Song song =
               FlatRedBallServices.Load<Song>(@"Content/MainMusic", contentManagerName);

            Microsoft.Xna.Framework.Media.MediaPlayer.Volume = 0.1f;
            Microsoft.Xna.Framework.Media.MediaPlayer.Play(song);


            hidinglayer = SpriteManager.AddLayer();
            obstaclelayer = SpriteManager.AddLayer();
            itemlayer = SpriteManager.AddLayer();
            playerlayer = SpriteManager.AddLayer();
            effectslayer = SpriteManager.AddLayer();
            hidingeffectslayer = SpriteManager.AddLayer();
            birdlayer = SpriteManager.AddLayer();
            //Make new layer for HUD elements
            hudlayer = SpriteManager.AddLayer();
            hudlayer.UsePixelCoordinates();
            HUD.SetupDigits(ContentManagerName);
            SecondHUD.SetupSecondDigits(ContentManagerName);

            SpriteManager.AddToLayer(HUD.HudFrame,hudlayer);

            foreach (HudNumber h in HUD.ScoreDigits)
            {
                SpriteManager.AddToLayer(h.Number, hudlayer);
            }
            foreach (HudNumber h in HUD.Multiplier)
            {
                SpriteManager.AddToLayer(h.Number, hudlayer);
            }
            foreach (HudNumber h in HUD.BagValue)
            {
                SpriteManager.AddToLayer(h.Number, hudlayer);
            }
            foreach (HudNumber h in HUD.CurrentWeight)
            {
                SpriteManager.AddToLayer(h.Number, hudlayer);
            }
            foreach (HudNumber h in HUD.MaxWeight)
            {
                SpriteManager.AddToLayer(h.Number, hudlayer);
            }
            if (!isTwoPlayers)
            {
                foreach (HudNumber h in HUD.Timer)
                {
                    SpriteManager.AddToLayer(h.Number, hudlayer);
                }
            }

            for (int i = 0; i < ShadowCount; i++)
            {
                Console.WriteLine(i);
                Entities.Shadow newShadow = new Shadow(this.ContentManagerName, true);
                newShadow.moveOnScreen();
                newShadow.CurrentState = Shadow.VariableState.BigShadow;
                SpriteManager.AddToLayer(newShadow.ShadowSprite, birdlayer);
                Shadows.Add(newShadow);
            }
            SpriteManager.AddToLayer(this.Bird.BirdSprite, birdlayer);
            setupBoundaries();
            if (isTwoPlayers)
            {

                SpriteManager.AddToLayer(SecondHUD.HudFrame, hudlayer);

                foreach (HudNumber h in SecondHUD.ScoreDigits)
                {
                    SpriteManager.AddToLayer(h.Number, hudlayer);
                }
                foreach (HudNumber h in SecondHUD.Multiplier)
                {
                    SpriteManager.AddToLayer(h.Number, hudlayer);
                }
                foreach (HudNumber h in SecondHUD.BagValue)
                {
                    SpriteManager.AddToLayer(h.Number, hudlayer);
                }
                foreach (HudNumber h in SecondHUD.CurrentWeight)
                {
                    SpriteManager.AddToLayer(h.Number, hudlayer);
                }
                foreach (HudNumber h in SecondHUD.MaxWeight)
                {
                    SpriteManager.AddToLayer(h.Number, hudlayer);
                }
            }
            adjustNumbers((int)LevelTimer, HUD.Timer);
            HUD.Timer[0].NumberScaleX = 10.5f;
            HUD.Timer[0].NumberScaleY = 18;
            HUD.Timer[1].NumberScaleX = 10.5f;
            HUD.Timer[1].NumberScaleY = 18;
            TIME_RESPAWN_TIMER = FlatRedBallServices.Random.Next(MIN_TIME_RESPAWN, MAX_TIME_RESPAWN);

            //Reset global data for level-related fields
            GlobalData.Score = 0;
            GlobalData.P2Score = 0;
            GlobalData.is2P = isTwoPlayers;
        }

        public void adjustHUD(HUD hud)
        {
            if (hud.Equals(HUD))
            {
                //Initializing hud
                int score = (int)PlayerInstances[0].Score;
                // Console.WriteLine("Score " + score + " Multiplier" + Multiplier + " BVal " + MainCharacterInstance.BagValue + " BWeight " + MainCharacterInstance.BagWeight + " Max Weight" + MAX_WEIGHT);
                adjustNumbers(score, hud.ScoreDigits);
                adjustNumbers((int)PlayerInstances[0].Multiplier, hud.Multiplier);
                adjustNumbers((int)PlayerInstances[0].BagValue, hud.BagValue);
                adjustNumbers((int)PlayerInstances[0].BagWeight, hud.CurrentWeight);
                adjustNumbers((int)MAX_WEIGHT, HUD.MaxWeight);
                //Console.WriteLine("Score " + score + " Multiplier" + Multiplier + " BVal " + MainCharacterInstance.BagValue + " BWeight " + MainCharacterInstance.BagWeight + " Max Weight" + MAX_WEIGHT);
                adjustNumbers(score, hud.ScoreDigits);
                adjustNumbers((int)PlayerInstances[0].Multiplier, hud.Multiplier);
                adjustNumbers((int)PlayerInstances[0].BagValue, hud.BagValue);
                adjustNumbers((int)PlayerInstances[0].BagWeight, hud.CurrentWeight);
                adjustNumbers((int)MAX_WEIGHT, hud.MaxWeight);
            }
            else
            {
                //Initializing hud
                int score = (int)PlayerInstances[1].Score;
                // Console.WriteLine("Score " + score + " Multiplier" + Multiplier + " BVal " + MainCharacterInstance.BagValue + " BWeight " + MainCharacterInstance.BagWeight + " Max Weight" + MAX_WEIGHT);
                adjustNumbers(score, hud.ScoreDigits);
                adjustNumbers((int)PlayerInstances[1].Multiplier, hud.Multiplier);
                adjustNumbers((int)PlayerInstances[1].BagValue, hud.BagValue);
                adjustNumbers((int)PlayerInstances[1].BagWeight, hud.CurrentWeight);
                adjustNumbers((int)MAX_WEIGHT, hud.MaxWeight);
                //Console.WriteLine("Score " + score + " Multiplier" + Multiplier + " BVal " + MainCharacterInstance.BagValue + " BWeight " + MainCharacterInstance.BagWeight + " Max Weight" + MAX_WEIGHT);
                adjustNumbers(score, hud.ScoreDigits);
                adjustNumbers((int)PlayerInstances[1].Multiplier, hud.Multiplier);
                adjustNumbers((int)PlayerInstances[1].BagValue, hud.BagValue);
                adjustNumbers((int)PlayerInstances[1].BagWeight, hud.CurrentWeight);
                adjustNumbers((int)MAX_WEIGHT, hud.MaxWeight);
            }
        }

        public void CustomActivity(bool firstTimeCalled)
        {
            if (MoveToMainMenu)
            {
                LevelBase.MoveToMainMenu = false;
                MoveToScreen(typeof(Gnome.Screens.MainMenu).FullName);
            }
            if (!isPaused)
            {
                checkGrassRustle();

                foreach (GnomePlayer gnome in PlayerInstances)
                {
                    if (gnome.collision_timer < 0)
                    {
                        //If the character is hiding, don't bother checking the shadows for collision.
                        if (!gnome.isHiding)
                        {
                            if (!DEBUG)
                                checkShadows(gnome);
                        }
                    }
                    else
                        gnome.collision_timer--;

                    checkHidingPlaces(gnome);

                    //THIS IS FOR CHECKING IF THE SCREEN DOES NOT HAVE A FULL COUNT OF ITEMS
                    if (Items.Count < this.MaxItems && ITEM_RESPAWN_TIMER <= 0)
                    {
                        //Get a random position
                        Vector3 itempos = getRandomPosition();
                        //Make a new item with this position
                        Item newitem = ItemFactory.get(itempos, this.ContentManagerName);
                        SpriteManager.AddToLayer(newitem.ItemSprite, itemlayer);
                        Items.Add(newitem);
                        ITEM_RESPAWN_TIMER = FlatRedBallServices.Random.Next(10, 100);
                    }
                    else if (Items.Count < this.MaxItems)
                        ITEM_RESPAWN_TIMER--;

                    checkItems(gnome);

                    if (PowerUps.Count < MaxPowerups && PWRUP_RESPAWN_TIMER <= 0)
                    {
                        Vector3 puppos = getRandomPosition();
                        PowerUp newpup = PowerUpFactory.get(puppos, this.ContentManagerName);
                        SpriteManager.AddToLayer(newpup.PowerUpSprite, itemlayer);
                        PowerUps.Add(newpup);
                        PWRUP_RESPAWN_TIMER = FlatRedBallServices.Random.Next(MIN_POWERUP_SEC, MAX_POWERUP_SEC);
                    }
                    else if (PowerUps.Count < this.MaxPowerups)
                        PWRUP_RESPAWN_TIMER -= TimeManager.SecondDifference;

                    checkPowerup(gnome);

                    modifyMovement(gnome);

                    checkHomeBase(gnome, gnome.Home);

                    if (gnome.isDead)
                    {

                        //Check for collisions between the gnome and the bird.
                        // Only do this check if the player is dead and he hasn't already been caught by the bird.
                        if (!playerCaughtByBird && gnome.CollisionBox.CollideAgainst(Bird.CollisionBox))
                        {
                            //MainCharacterInstance.Y = Bird.Y;
                            gnome.AttachTo(Bird, true);

                            // bogus formula, see comment in method SwoopDownAndKillTheGnomePlayerLikeTheLittleBitchHeIs()
                            // for explaination
                            float birdScaleFactor = (5f / (Bird.Y + (SCREEN_HEIGHT / 2f)) * 30f);
                            Bird.setScaleVelocity(birdScaleFactor);
                            playerCaughtByBird = true;
                            Bird.changeAnimations();
                        }
                        // if the bird has moved off the screen then the death animation is over and return to the main menu.
                        else if (Bird.Y < (SCREEN_HEIGHT / -2) - 10)
                        {
                            MoveToScreen(typeof(Gnome.Screens.EndScreen).FullName);
                        }
                        else if (playerCaughtByBird)
                        {
                            endgame_counter--;
                            if (endgame_counter <= 0)
                            {
                                MoveToScreen(typeof(Gnome.Screens.EndScreen).FullName);
                            }
                        }
                    }

                    checkBoundaries(gnome);

                    //Drop the gnome's bag if the player presses the button.
                    if (CustomInputManager.isDropButtonPushed(true))
                        checkDrop(gnome);
                }
                if (!isTwoPlayers)
                {
                    if (LevelTimer < 0)
                    {
                        GlobalData.Score = (int)PlayerInstances[0].Score;
                        GlobalData.P2Score = 0;
                        GlobalData.ClearedLevel = PlayerInstances[0].Score > TARGET_SCORE;
                        MoveToScreen(typeof(Gnome.Screens.EndScreen).FullName);
                    }
                    else
                    {
                        int oldtime = (int)LevelTimer;
                        LevelTimer -= TimeManager.SecondDifference;
                        if ((int)LevelTimer != oldtime)
                        {
                            adjustNumbers((int)LevelTimer, HUD.Timer);
                            HUD.Timer[0].NumberScaleX = 10.5f;
                            HUD.Timer[0].NumberScaleY = 18;
                        }
                        //Check for spawning new time pickups
                        if (TimePickups.Count < MAX_TIME_PICKUPS && TIME_RESPAWN_TIMER <= 0)
                        {
                            Vector3 pos = getRandomPosition();
                            Item newclock = ItemFactory.get(Item.VariableState.Clock, pos, ContentManagerName);
                            newclock.SetupDespawnTimer();
                            TimePickups.Add(newclock);
                            SpriteManager.AddToLayer(newclock.ItemSprite, itemlayer);
                            TIME_RESPAWN_TIMER = FlatRedBallServices.Random.Next(MIN_TIME_RESPAWN, MAX_TIME_RESPAWN);
                        }
                        else if (TimePickups.Count < MAX_TIME_PICKUPS)
                            TIME_RESPAWN_TIMER -= TimeManager.SecondDifference;
                    }
                }
            }

            if (CustomInputManager.isPauseButtonPushed(isPaused))
            {
                togglePause();
            }

            if (!MoveToMainMenu && !isPaused)
            {

            }

            if (isTwoPlayers)
            {
                //secondPActivity();
            }
        }

        private void adjustNumbers(int score, FlatRedBall.Math.PositionedObjectList<HudNumber> list)
        {
            String s = score.ToString();
            int numZeros = list.Count - s.Length;
            for (int i = 0; i < numZeros; i++)
            {
                s = "0" + s;
            }
            for (int i = s.Length - 1; i >= 0; i--)
            {
                //int fuck = (int)(Score / ((int)Math.Pow(10, (int)(Math.Log10(Score) + 1) - i)) % 10);
                list[i].NumberScaleX = 5.25f;
                list[i].NumberScaleY = 9f;
                switch (s[i])
                {
                    case '0':
                        list[i].Number.CurrentChainName = "Zero";
                        break;
                    case '1':                        
                        list[i].Number.CurrentChainName = "One";
                        break;
                    case '2':                        
                        list[i].Number.CurrentChainName = "Two";
                        break;
                    case '3':                        
                        list[i].Number.CurrentChainName = "Three";
                        break;
                    case '4':                        
                        list[i].Number.CurrentChainName = "Four";
                        break;
                    case '5':                        
                        list[i].Number.CurrentChainName = "Five";
                        break;
                    case '6':                        
                        list[i].Number.CurrentChainName = "Six";
                        break;
                    case '7':                        
                        list[i].Number.CurrentChainName = "Seven";
                        break;
                    case '8':                        
                        list[i].Number.CurrentChainName = "Eight";
                        break;
                    case '9':                        
                        list[i].Number.CurrentChainName = "Nine";
                        break;
                    default:
                        list[i].Number.CurrentChainName = "Zero";
                        break;
                }
            }
        }
        /*
        private void secondPActivity()
        {
            if (!isPaused)
            {
                checkHidingPlaces(SecondCharacterInstance);

                //Check for collisions between gnome and items. For a collision, pick up the item and destroy its entity
                checkItems(SecondCharacterInstance);
                checkPowerup(SecondCharacterInstance);
                //Check if a player has won.
                checkScoreLimit();

                //Check each obstacle for collisions, if the player collides, modify his speed by the speed modifier
                modifyMovement(SecondCharacterInstance);

                if (collision_timer_2 < 0)
                {
                    //If the character is hiding, don't bother checking the shadows for collision.
                    if (!SecondCharacterInstance.isHiding)
                    {
                        checkShadows(SecondCharacterInstance);
                    }
                }
                else
                    collision_timer_2--;

                checkHomeBase(SecondCharacterInstance, SecondHomeBaseInstance);

                //THIS IS GOING TO CAUSE PROBLEMS
                if (SecondCharacterInstance.isDead)
                {

                    //Check for collisions between the gnome and the bird.
                    // Only do this check if the player is dead and he hasn't already been caught by the bird.
                    if (!playerCaughtByBird && SecondCharacterInstance.CollisionBox.CollideAgainst(Bird.CollisionBox))
                    {
                        //MainCharacterInstance.Y = Bird.Y;
                        SecondCharacterInstance.AttachTo(Bird, true);

                        // bogus formula, see comment in method SwoopDownAndKillTheGnomePlayerLikeTheLittleBitchHeIs()
                        // for explaination
                        float birdScaleFactor = (5f / (Bird.Y + (SCREEN_HEIGHT / 2f)) * 30f);
                        Bird.setScaleVelocity(birdScaleFactor);
                        playerCaughtByBird = true;
                        Bird.changeAnimations();
                    }
                    // if the bird has moved off the screen then the death animation is over and return to the main menu.
                    else if (Bird.Y < (SCREEN_HEIGHT / -2) - 10)
                    {
                        MoveToScreen(typeof(Gnome.Screens.EndScreen).FullName);
                    }
                    else if (playerCaughtByBird)
                    {
                        endgame_counter--;
                        if (endgame_counter <= 0)
                            MoveToScreen(typeof(Gnome.Screens.EndScreen).FullName);
                    }
                }

                //Check for collsions between the gnome and the outside boundaries.
                checkBoundaries(SecondCharacterInstance);

                //Drop the gnome's bag if the player presses the button.
                if (CustomInputManager.isDropButtonPushed(false))
                    checkDrop(SecondCharacterInstance);
            }
        }
        */
        /* 
         * Checks each hiding place for collisions and proximity.
         * Handles appropriately.
         * */
        protected void checkHidingPlaces(GnomePlayer player)
        {
            if (player.isDead)
                return;

            bool someoneWantsToHide = CustomInputManager.isHideButtonPushed(!player.isSecondPlayer);

            //THIS IS FOR CHECKING IF THE PLAYER IS NEAR ANY HIDING SPOT
            foreach (HidingSpot hidingspot in HidingSpots)
            {
                hidingspot.CollisionBox.CollideAgainstMove(player.CollisionBox, 10, 0);
                bool hidingspot_colliding = collidingWithAnyGnomes(hidingspot);
                if (hidingspot_colliding == true)
                {
                    if (!hidingspot.inUse && !player.isDead)
                    {
                        //hidingspot.AppleTreeSprite.Alpha = 0.6f;
                        //hidingspot.EffectSprite.Alpha = 0.6f;
                        hidingspot.GlowSprite.Visible = true;
                        hidingspot.EffectGlowSprite.Visible = true;
                    }
                    else if (player.isDead)
                    {
                        hidingspot.GlowSprite.Visible = false;
                        hidingspot.EffectGlowSprite.Visible = false;
                    }
                }
                else
                {
                    //hidingspot.AppleTreeSprite.Alpha = 1.0f;
                    //hidingspot.EffectSprite.Alpha = 1.0f;
                    hidingspot.GlowSprite.Visible = false;
                    hidingspot.EffectGlowSprite.Visible = false;
                }
                if(hidingspot.CollisionRadius.CollideAgainst(player.CollisionBox))
                {
                    if (someoneWantsToHide)
                    {
                        hide(hidingspot, player);

                        hidingspot.shake();
                    }
                }
            }
        }

        /*
         * Check for collision with items.
         * */
        protected void checkItems(GnomePlayer player)
        {
            // If the player is dead, don't bother doing collision detection for items.
            foreach (GnomePlayer gnome in PlayerInstances)
            {
                if (player.isDead)
                    return;
            }
            if (player.isHiding)
                return;
            //Check for collisions between gnome and items. For a collision, pick up the item and destroy its entity
            foreach (Item item in Items)
            {
                if (player.BagWeight < MAX_WEIGHT &&
                    item.CollisionBox.CollideAgainst(player.CollisionBox) &&
                    item.pickupTimer <= 0 &&
                    (player.canPickup || (item.Owner != null && item.Owner.Equals(player))))
                {
                    Sound pickup = AudioManager.GetSound("pickup2");
                    Sound rarepickup = AudioManager.GetSound("pickup1");
                    if (item.CurrentState.Equals(Item.VariableState.Hidden))
                    {
                        rarepickup.Play();
                    }
                    else
                    {
                        pickup.Play();
                    }

                    player.BagValue += item.itemvalue;
                    player.BagWeight += item.itemweight;

                    adjustHUD(player.HUD);

                    item.Destroy();
                }
                else if (player.BagWeight >= MAX_WEIGHT &&
                    item.CollisionBox.CollideAgainst(player.CollisionBox))
                {
                    player.Overburdened.Visible = true;
                    player.Overburdened.Animate = true;
                }
            }

            foreach (Item clock in TimePickups)
            {
                if (player.CollisionBox.CollideAgainst(clock.CollisionBox))
                {
                    Sound pickup = AudioManager.GetSound("pickup2");
                    pickup.Play();
                    LevelTimer += TIME_ADDED;
                    if (LevelTimer > MAX_LEVEL_TIME)
                        LevelTimer = MAX_LEVEL_TIME;
                    adjustNumbers((int)LevelTimer, HUD.Timer);
                    HUD.Timer[0].NumberScaleX = 10.5f;
                    HUD.Timer[0].NumberScaleY = 18;
                    HUD.Timer[1].NumberScaleX = 10.5f;
                    HUD.Timer[1].NumberScaleY = 18;
                    clock.Destroy();
                }
            }
        }

        protected void checkPowerup(GnomePlayer player)
        {
            // If the player is dead, don't bother doing collision detection for items.
            foreach (GnomePlayer gnome in PlayerInstances)
            {
                if (player.isDead)
                    return;
            }
            if (player.isHiding)
                return;
            //Check for collisions between gnome and items. For a collision, pick up the item and destroy its entity
            foreach (PowerUp pwr in PowerUps)
            {
                /*if (player.BagWeight < MAX_WEIGHT &&
                    item.CollisionBox.CollideAgainst(player.CollisionBox) &&
                    item.pickupTimer <= 0 &&
                    (player.canPickup || (item.Owner != null && item.Owner.Equals(player))))
                {*/
                if(pwr.CollisionBox.CollideAgainst(player.CollisionBox))
                {
                    Sound pickup = AudioManager.GetSound("pickup2");
                    pickup.Play();

                    player.PowerUp = pwr.CurrentState;
                    player.PowerUpTimer = pwr.Duration;

                    pwr.Destroy();
                }
                    /*
                else if (player.BagWeight >= MAX_WEIGHT &&
                    item.CollisionBox.CollideAgainst(player.CollisionBox))
                {
                    player.Overburdened.Visible = true;
                    player.Overburdened.Animate = true;
                }*/
            }
        }

        /*
         * Check for speed and movement modification based on terrain and bag weight
         * */
        protected void modifyMovement(GnomePlayer player)
        {
            float speedmodifier = 1;
            player.OnTerrain = Obstacle.VariableState.None;
            foreach (Obstacle o in Obstacles)
            {
                // If the player is dead, don't bother doing collision detection for items.
                if (player.isDead)
                    break;
                if (o.Collision.CollideAgainst(player.CollisionBox))
                {
                    player.OnTerrain = o.CurrentState;
                    speedmodifier = o.SpeedModifier;
                    player.OnTerrain = o.CurrentState;
                    //Need to check this for both players in the same call
                }
            }

            //Certain powerups affect speed modifier.
            if (player.PowerUp == PowerUp.VariableState.SpeedUp)
                speedmodifier *= 2;

            //Affect the player's move speed based on bag weight.
            if (MAX_WEIGHT * 0.33 < player.BagWeight &&
                player.BagWeight <= MAX_WEIGHT * 0.67)
            {
                player.moveSpeed = GnomePlayer.MAX_SPEED * 0.75f * speedmodifier;
                player.BagSize = "Medium";
            }
            else if (player.BagWeight > MAX_WEIGHT * 0.67)
            {
                player.moveSpeed = GnomePlayer.MAX_SPEED * 0.5f * speedmodifier;
                player.BagSize = "Large";
            }
            else
            {
                player.moveSpeed = GnomePlayer.MAX_SPEED * speedmodifier;
                player.BagSize = "Small";
            }
            if (!player.canPickup)
            {
                player.BagSize = "None";
            }
        }

        public void checkGrassRustle()
        {
            foreach (Obstacle o in Obstacles)
            {
                List<GnomePlayer> gnomes = collidingWithAnyGnomes(o);
                if (o.CurrentState == Obstacle.VariableState.TallGrass &&
                    gnomes.Count > 0)
                {
                    foreach (GnomePlayer gnome in gnomes)
                    {
                        if (gnome.XVelocity != 0 || gnome.YVelocity != 0)
                        {
                            if (!grass.IsPlaying)
                            {
                                grass.Play();
                            }
                            if (o.VisRep.CurrentChainIndex != 1)
                            {
                                o.VisRep.CurrentChainIndex = 1;
                                o.VisRep.Animate = true;
                            }
                            o.OverlayEffect.Visible = true;
                            o.OverlayEffect.Animate = true;
                            break;
                        }
                        else 
                        {
                            o.VisRep.CurrentChainIndex = 0;
                            o.OverlayEffect.Animate = false;
                            o.OverlayEffect.CurrentFrameIndex = 0;
                        }
                    }
                }
                else if (o.CurrentState == Obstacle.VariableState.TallGrass && o.VisRep.CurrentChainIndex != 0)
                {
                    o.VisRep.CurrentChainIndex = 0;
                    //o.OverlayEffect.Animate = false;
                    o.OverlayEffect.CurrentFrameIndex = 0;
                    o.OverlayEffect.Visible = false;
                }
            }
        }

        private List<GnomePlayer> collidingWithAnyGnomes(Obstacle o)
        {
            List<GnomePlayer> gnomes = new List<GnomePlayer>();
            foreach (GnomePlayer gnome in PlayerInstances)
            {
                if (o.Collision.CollideAgainst(gnome.CollisionBox))
                {
                    gnomes.Add(gnome);
                }
            }
            return gnomes;
        }

        private bool collidingWithAnyGnomes(HidingSpot o)
        {
            bool colliding = false;
            foreach (GnomePlayer gnome in PlayerInstances)
            {
                if (o.CollisionRadius.CollideAgainst(gnome.CollisionBox))
                {
                    colliding = true;
                    break;
                }
            }
            return colliding;
        }

        /*
         * Check for collision between shadows and players
         * */
        protected void checkShadows(GnomePlayer player)
        {
            // If the player is dead, don't bother checking for collision
            if (player.isDead)
                return;

            foreach (Shadow shadow in Shadows)
            {
                if (shadow.CollisionBox.CollideAgainst(player.CollisionBox))
                {
                    if (shadow.CurrentState == Shadow.VariableState.BigShadow)
                    {
                        squak4.Play();
                        shadow.CurrentState = Shadow.VariableState.MediumShadow;
                        shadow.setScaleTarget(Shadow.SHADOW_RESOLUTIONS[1]);
                        double x = ((double)Shadow.SHADOW_RESOLUTIONS[1] / Shadow.SHADOW_RESOLUTIONS[0]);
                        shadow.CollisionBox.ScaleBy(x);
                        shadow.CollisionBox.RelativeY -= 0.2f;
                    }
                    else if (shadow.CurrentState == Shadow.VariableState.MediumShadow)
                    {
                        squak1.Play();
                        shadow.CurrentState = Shadow.VariableState.SmallShadow;
                        shadow.setScaleTarget(Shadow.SHADOW_RESOLUTIONS[2]);
                        double x = ((double)Shadow.SHADOW_RESOLUTIONS[2] / Shadow.SHADOW_RESOLUTIONS[1]);
                        shadow.CollisionBox.ScaleBy(x);
                        shadow.CollisionBox.RelativeY -= 0.3f;
                    }
                    //else
                    //kill player
                    else
                    {
                        squak3.Play();
                        player.isDead = true;
                        SwoopDownAndKillTheGnomePlayerLikeTheLittleBitchHeIs();
                        

                        if(isTwoPlayers)
                        {
                            foreach(GnomePlayer gnome in PlayerInstances)
                            {
                                if (player.Equals(gnome))
                                {
                                    gnome.Score = -1;
                                }
                            }
                            GlobalData.P2Score = (int)PlayerInstances[1].Score;
                        }
                        GlobalData.Score = (int)PlayerInstances[0].Score;
                        shadow.Y = 3 * SCREEN_HEIGHT;
                    }
                        player.collision_timer = COLLISION_WAIT;
                }
            }
        }

        /*
         * Checks for collisions and interactions with the homebase and its door
         * */
        protected void checkHomeBase(GnomePlayer player, HomeBase checkbase)
        {
            bool onedead = false;
            foreach (GnomePlayer gnome in PlayerInstances)
            {
                gnome.CollisionBox.CollideAgainstMove(checkbase.CollisionBox, 0, 1);
                if (gnome.isDead)
                    onedead = true;
            }
            if (onedead == true)
                return;
            if ((checkbase.Door.CollisionRadius.CollideAgainst(player.CollisionBox))
                && checkbase.Door.inUse == false)
            {
                checkbase.GlowSprite.Visible = true;
                checkbase.HomeBaseSprite.Visible = false;
            }
            else
            {
                checkbase.HomeBaseSprite.Visible = true;
                checkbase.GlowSprite.Visible = false;
            }
            if (player.CollisionBox.CollideAgainst(checkbase.Door.CollisionRadius))
            {
                if (CustomInputManager.isHideButtonPushed(!player.isSecondPlayer))
                {
                    hide(checkbase.Door, player);
                    //Adjust Multiplier
                    if (player.isHiding)
                    {
                        if (player.BagWeight >= MAX_WEIGHT)
                        {
                            player.Multiplier += 2;
                        }
                        else if (player.BagWeight >= MAX_WEIGHT / 2)
                        {
                            player.Multiplier += 1.5f;
                        }
                        else
                        {
                            player.Multiplier = 1;
                        }

                        //Bounding the Multiplier by MAX_MULTIPLIER

                        player.Multiplier = (player.Multiplier > MAX_MULTIPLIER ? MAX_MULTIPLIER : player.Multiplier);

                        if (player.BagValue > 0)
                        {
                            dropoff.Play();
                            player.BagWeight = 0;
                            player.Score += (float)Math.Round((double)(player.BagValue * player.Multiplier));
                            if (player.Score > MAX_SCORE)
                                player.Score = MAX_SCORE;
                            player.BagValue = 0;
                            player.moveSpeed = GnomePlayer.MAX_SPEED;
                        }
                    }

                    adjustHUD(player.HUD);
                }
            }
        }



        /*
         * Check for collision between player and the level's boundaries
         * */
        protected void checkBoundaries(GnomePlayer player)
        {
            foreach (AxisAlignedRectangle boundary in BoundaryRectangles)
            {
                // If the character is dead, don't bother checking for collisions
                if (player.isDead)
                    break;
                if (player.CollisionBox.CollideAgainstMove(boundary, 0, 1) &&
                    player.OnTerrain == Obstacle.VariableState.Mud)
                {
                    if (boundary.Y > 0 || boundary.Y < 0)
                        player.YVelocity = 0;
                    else
                        player.XVelocity = 0;
                }
            }
        }

        protected void checkDrop(GnomePlayer player)
        {
            if (player.canPickup && player.BagWeight > 0)
            {
                Item bag = player.DropBag();
                SpriteManager.AddToLayer(bag.ItemSprite, itemlayer);
                SpriteManager.AddToLayer(bag.CountdownSprite, effectslayer);
                Items.Add(bag);
                adjustHUD(player.HUD);
            }
        }

        protected void checkScoreLimit()
        {
            foreach (GnomePlayer player in PlayerInstances)
            {
                if (player.Score >= TARGET_SCORE)
                {
                    GlobalData.Score = Math.Min( (int)PlayerInstances[0].Score, TARGET_SCORE );
                    GlobalData.P2Score = Math.Min((int)PlayerInstances[1].Score, TARGET_SCORE);
                    MoveToScreen(typeof(Gnome.Screens.ScoreScreen).FullName);
                }
            }
        }
        public void CustomDestroy()
        {
            foreach (GnomePlayer gnome in PlayerInstances)
                gnome.Destroy();
            this.HomeBaseInstance.Destroy();
            this.HUD.Destroy();
            this.Bird.Destroy();
            foreach (HidingSpot h in this.HidingSpots)
            {
                h.Destroy();
            }
            foreach (Obstacle o in this.Obstacles)
            {
                o.Destroy();
            }
            foreach (Item i in this.Items)
            {
                i.Destroy();
            }
            foreach (Shadow s in this.Shadows)
            {
                s.Destroy();
            }
            /*
            foreach (AxisAlignedRectangle rect in BoundaryRectangles)
            {
                ShapeManager.Remove(rect);
            }
             */
            SpriteManager.RemoveLayer(playerlayer);
            SpriteManager.RemoveLayer(birdlayer);
            SpriteManager.RemoveLayer(obstaclelayer);
            SpriteManager.RemoveLayer(hidinglayer);
            SpriteManager.RemoveLayer(itemlayer);
            SpriteManager.RemoveLayer(effectslayer);
            SpriteManager.RemoveLayer(hidingeffectslayer);
            SpriteManager.RemoveLayer(hudlayer);
        }



        /*
         * Gets a random position to spawn a new item at. This position is checked to make sure it
         * is not too close to the player, other items, or hiding spots.
         */
        protected Vector3 getRandomPosition()
        {
            while (true)
            {
                int widthfactor = (int)(SCREEN_WIDTH / 2 - ITEM_SPRITE_WIDTH);
                int heightfactor = (int)(SCREEN_HEIGHT / 2 - ITEM_SPRITE_HEIGHT);
                float x = (float)FlatRedBallServices.Random.Next(-widthfactor, widthfactor);
                float y = (float)FlatRedBallServices.Random.Next(-heightfactor, heightfactor);
                Vector3 newpos = new Vector3(x, y, 1);
                if (!isNearObject(newpos))
                    return newpos;
            }
        }

        /*
         * Checks whether the given position is within an arbitrary distance of any of the
         * entities we want to check for
         */
        protected bool isNearObject(Vector3 position)
        {
            foreach (GnomePlayer gnome in PlayerInstances)
            {
                if (distanceBetween(position, gnome.Position) < 5)
                    return true;
            }
            foreach (Item item in Items)
            {
                if (distanceBetween(position, item.Position) < 3)
                    return true;
            }

            foreach (HidingSpot spot in HidingSpots)
            {
                if (distanceBetween(position, spot.Position) < spot.CollisionRadius.Radius * 1.4f)
                    return true;
            }

            if (distanceBetween(position, HomeBaseInstance.Position) < 10)
                return true;

            if (distanceBetween(position, SecondHomeBaseInstance.Position) < 10)
                return true;

            return false;
        }

        protected float distanceBetween(Vector3 pos1, Vector3 pos2)
        {
            float xdiff = pos1.X - pos2.X;
            double xdistance = Math.Pow(xdiff, 2);
            float ydiff = pos1.Y - pos2.Y;
            double ydistance = Math.Pow(ydiff, 2);
            float distance = (float)Math.Sqrt(xdistance + ydistance);
            return distance;
        }

        protected void loadObjectsFromSprites(Scene map)
        {
            //Load obstacles
            foreach (Texture2D texture in obstacleTextures)
            {
                SpriteList sprites = customFindSpritesWithTexture(map.Sprites, texture.ToString());
                foreach (Sprite sprite in sprites)
                {
                    Obstacle newobstacle = new Obstacle(ContentManagerName, true);
                    newobstacle.CurrentState = obstacleTextureTypeMap[texture];
                    newobstacle.setupSprite(sprite);
                    SpriteManager.AddToLayer(newobstacle.VisRep, obstaclelayer);
                    if (newobstacle.HasOverlay)
                        SpriteManager.AddToLayer(newobstacle.OverlayEffect, effectslayer);
                    Obstacles.Add(newobstacle);
                }
            }
            //Hiding spots
            foreach (Texture2D texture in hidingTextures)
            {
                SpriteList sprites = customFindSpritesWithTexture(map.Sprites, texture.ToString());
                foreach (Sprite sprite in sprites)
                {
                    HidingSpot newspot = new HidingSpot(ContentManagerName);
                    newspot.Position = sprite.Position;
                    newspot.SetupSprite(sprite);
                    SpriteManager.AddToLayer(newspot.AppleTreeSprite, hidinglayer);
                    SpriteManager.AddToLayer(newspot.GlowSprite, hidinglayer);
                    newspot.AppleTreeSprite.CurrentChainIndex = 0;
                    newspot.AppleTreeSprite.Animate = true;
                    HidingSpots.Add(newspot);
                    SpriteManager.AddToLayer(newspot.EffectSprite, hidingeffectslayer);
                    SpriteManager.AddToLayer(newspot.EffectGlowSprite, hidingeffectslayer);
                    SpriteManager.AddToLayer(newspot.HidingEffectSprite, hidingeffectslayer);
                }
            }
            //Home base
            Texture2D basetexture = FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/dog-house.png");
            SpriteList spritel = customFindSpritesWithTexture(map.Sprites, basetexture.ToString());
            int gnomecount = 0;
            foreach (Sprite basesprite in spritel)
            {
                HomeBase newbase = new HomeBase(ContentManagerName);
                newbase.SetupSprite(basesprite);
                HomeBaseInstance = newbase;
                SpriteManager.AddToLayer(HomeBaseInstance.HomeBaseSprite, hidinglayer);
                SpriteManager.AddToLayer(HomeBaseInstance.GlowSprite, hidinglayer);
                GnomePlayer gnome = newbase.CreateGnome();
                PlayerInstances.Add(gnome);
                InitializeGnome(gnome, gnomecount);
                gnomecount += 1;
            }
            mSecondHomeBaseInstance = HomeBaseInstance;
            if (isTwoPlayers)
            {
                basetexture = FlatRedBallServices.Load<Texture2D>(@"Content/Screens/LevelOne/Images/dog-house-two.png");
                spritel = customFindSpritesWithTexture(map.Sprites, basetexture.ToString());
                foreach (Sprite basesprite in spritel)
                {
                    HomeBase newbase = new HomeBase(ContentManagerName);
                    basesprite.ScaleX = HomeBaseInstance.HomeBaseSprite.ScaleX;
                    basesprite.ScaleY = HomeBaseInstance.HomeBaseSprite.ScaleY;
                    newbase.SetupSecondSprite(basesprite);
                    mSecondHomeBaseInstance = newbase;
                    SpriteManager.AddToLayer(SecondHomeBaseInstance.HomeBaseSprite, hidinglayer);
                    SpriteManager.AddToLayer(SecondHomeBaseInstance.GlowSprite, hidinglayer);
                    GnomePlayer gnome = newbase.CreateGnome();
                    PlayerInstances.Add(gnome);
                    InitializeGnome(gnome, gnomecount);
                    gnomecount += 1;
                }
            }
        }

        private GnomePlayer InitializeGnome(GnomePlayer inputgnome, int playernum)
        {
            inputgnome.setSprite(GlobalData.PlayerColors[playernum]);

            if (playernum > 0)
            {
                inputgnome.isSecondPlayer = true;
                inputgnome.HUD = SecondHUD;
            }
            else
            {
                inputgnome.isSecondPlayer = false;
                inputgnome.HUD = HUD;
            }

            adjustHUD(inputgnome.HUD);

            inputgnome.Position = new Vector3(inputgnome.Home.X,
                inputgnome.Home.Y - inputgnome.Home.HomeBaseSprite.ScaleY - inputgnome.GnomeSprite.ScaleY / 2,
                inputgnome.Z);

            inputgnome.SetController();
            SpriteManager.AddToLayer(inputgnome.MudSplash, effectslayer);
            SpriteManager.AddToLayer(inputgnome.Overburdened, effectslayer);
            SpriteManager.AddToLayer(inputgnome.DustCloud, effectslayer);
            SpriteManager.AddToLayer(inputgnome.GnomeSprite, playerlayer);
            return inputgnome;
        }

        private SpriteList customFindSpritesWithTexture(SpriteList sprites, String texture)
        {
            SpriteList returnsprites = new SpriteList();
            foreach (Sprite sprite in sprites)
            {
                if (sprite.Texture.ToString() == texture)
                    returnsprites.Add(sprite);
            }
            return returnsprites;
        }


        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }
        // Kills the player and ends the game
        private void SwoopDownAndKillTheGnomePlayerLikeTheLittleBitchHeIs()
        {
            foreach(GnomePlayer gnome in PlayerInstances)
            {
                if (gnome.isDead)
                {
                    // position the bird to attack and then have him swoop down.
                    Bird.X = gnome.X;
                    Bird.moveSpeed = 15;
                    // Stop all activity for the player and the birds.
                    foreach (Shadow shadow in Shadows)
                    {
                        shadow.isDead = true;
                    }
                    SpriteManager.RemoveSprite(gnome.MudSplash);
                    SpriteManager.RemoveSprite(gnome.Overburdened);

                    // Okay so this forumla is a lot of bull shit but basically,
                    // we had to figure out how to get the bird to scale to the same size
                    // when it hit the player despite the player's location.  Someone might
                    // wanna make this better.
                    //
                    //
                    // Here's the forumla we came up with:
                    // sizeOfBird / (distanceBetweenBirdAndGnome * sizeOfGnome) = scale facor
                    float birdScaleFactor = (Bird.moveSpeed * (Bird.BirdSprite.ScaleX - 3f)) / (Bird.Y - gnome.Y);
                    Bird.setScaleVelocity(-birdScaleFactor);
                    return;
                }
            }
        }

        public void hide(HidingSpot hidingspot, GnomePlayer player)
        {
            if (hidingspot.inUse && !player.isHiding)
                return;
            if (player.isDead)
                return;
            Sound treebush = AudioManager.GetSound("tree-bush");
            treebush.Play();
            if (hidingspot.inUse && player.isHiding)
            {
                hidingspot.HidingEffectSprite.Visible = false;
                hidingspot.AppleTreeSprite.CurrentChainIndex = 0;
                hidingspot.AppleTreeSprite.Animate = true;
                hidingspot.EffectSprite.Visible = true;
                player.GnomeSprite.Alpha = 1.0f;
                player.GnomeSprite.Animate = true;
                //If you want to see where the player will be once you hide,
                //and stop showing it once the player comes out of hiding,
                //uncomment this line:
                //player.CollisionBox.Visible = false;
                player.isHiding = false;
                hidingspot.inUse = false;
            }
            else
            {
                //hidingspot.AppleTreeSprite.Alpha = 1.0f;
                //hidingspot.EffectSprite.Alpha = 1.0f;
                hidingspot.GlowSprite.Visible = false;
                hidingspot.EffectGlowSprite.Visible = false;
                player.isHiding = true;
                hidingspot.inUse = true;
                player.GnomeSprite.Alpha = 0.3f;
                player.GnomeSprite.Animate = false;
                //If you want to see where the player will be once you hide,
                //and stop showing it once the player comes out of hiding,
                //uncomment this line:
                //player.CollisionBox.Visible = true;
                player.MudSplash.Visible = false;
                player.DustCloud.Visible = false;
                hidingspot.AppleTreeSprite.CurrentChainIndex = 1;
                hidingspot.AppleTreeSprite.Animate = true;
                hidingspot.HidingEffectSprite.Visible = true;
                hidingspot.EffectSprite.Visible = false;
            }
        }

        /*
         * Setup AxisAlignedRectangles at the edges of the level, just outside the camera.
         * */
        protected void setupBoundaries()
        {
            AxisAlignedRectangle newrect;
            //Top rectangle
            newrect = new AxisAlignedRectangle();
            newrect.Z = 1;
            newrect.ScaleX = SCREEN_WIDTH;
            newrect.ScaleY = 0f;
            newrect.Y = SCREEN_HEIGHT / 2;
            BoundaryRectangles.Add(newrect);
            //Bottom rectangle
            newrect = new AxisAlignedRectangle();
            newrect.Z = 1;
            newrect.ScaleX = SCREEN_WIDTH;
            newrect.ScaleY = 0f;
            newrect.Y = -SCREEN_HEIGHT / 2;
            BoundaryRectangles.Add(newrect);
            //Left rectangle
            newrect = new AxisAlignedRectangle();
            newrect.Z = 1;
            newrect.ScaleY = SCREEN_HEIGHT;
            newrect.ScaleX = 0f;
            newrect.X = -SCREEN_WIDTH / 2;
            BoundaryRectangles.Add(newrect);
            //Right rectangle
            newrect = new AxisAlignedRectangle();
            newrect.Z = 1;
            newrect.ScaleY = SCREEN_HEIGHT;
            newrect.ScaleX = 0f;
            newrect.X = SCREEN_WIDTH / 2;
            BoundaryRectangles.Add(newrect);
        }

        public void togglePause()
        {
            isPaused = !isPaused;
            foreach (GnomePlayer gnome in PlayerInstances)
                gnome.isPaused = isPaused;
            foreach (Shadow shadow in Shadows)
            {
                shadow.isPaused = isPaused;
            }
            if (isPaused && !pauseforinstructions)
            {
                InstructionManager.PauseEngine();
                ScreenManager.CurrentScreen.LoadPopup(typeof(Gnome.Screens.PauseScreen).FullName, true);
            }
            else
            {
                pauseforinstructions = false;
                InstructionManager.UnpauseEngine();
            }
        }
    }
}